#include "SpriteOpenGLES.h"

namespace YoghurtGum
{

	SpriteOpenGLES::SpriteOpenGLES(TextureOpenGLES* a_Tex) : Sprite()
	{
		m_Texture = a_Tex;
		m_Keying = false;
	}

	SpriteOpenGLES::~SpriteOpenGLES()
	{

	}

	void SpriteOpenGLES::SetAlphaKey(Color a_AlphaKey)
	{
		if (a_AlphaKey != Color::CNONE)
		{
			m_Keying = true;
			m_Key = a_AlphaKey;
		}	
	}

	bool SpriteOpenGLES::Draw(float a_X, float a_Y, float a_Depth /*= 0.f*/)
	{
		glShadeModel(GL_SMOOTH);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_LIGHTING);
		glDisable(GL_CULL_FACE);

		if (m_Keying)
		{
			glDisable(GL_BLEND);
			glEnable(GL_ALPHA_TEST); CGLE();
			glAlphaFuncx(GL_GREATER, 0); CGLE();
		}

		glEnable(GL_TEXTURE_2D); CGLE();
		glBindTexture(GL_TEXTURE_2D, m_Texture->GetID()); CGLE();

		glPushMatrix(); CGLE();
		glTranslatex(GlFloatToFixed(a_X), GlFloatToFixed(a_Y), 0); CGLE();

			glEnableClientState(GL_TEXTURE_COORD_ARRAY); CGLE();
			glEnableClientState(GL_VERTEX_ARRAY); CGLE();
				glVertexPointer(2, GL_FIXED, 0, m_Vertices); CGLE();
				glTexCoordPointer(2, GL_FIXED, 0, m_UVs); CGLE();
				glDrawArrays(GL_TRIANGLES, 0, 6); CGLE();
			glDisableClientState(GL_VERTEX_ARRAY); CGLE();
			glDisableClientState(GL_TEXTURE_COORD_ARRAY); CGLE();

		glPopMatrix(); CGLE();

		glDisable(GL_TEXTURE_2D); CGLE();

		if (m_Keying)
		{
			glDisable(GL_ALPHA_TEST); CGLE();
		}

		return true;
	}

	Surface* SpriteOpenGLES::ToSurface()
	{
		YG_STUB;
		return NULL;
	}

	bool SpriteOpenGLES::LoadImageData(til::Image* a_Image, const char* a_Name)
	{
		//Log::GetSingleton().Print("SpriteOpenGLES::LoadImageData (%s)", a_Name);

		if (strlen(a_Name) > 0)
		{ 
			strcpy(m_Name, a_Name); 
		}

		m_Image = a_Image;

		m_TextureWidth = a_Image->GetWidth();
		m_TextureHeight = a_Image->GetHeight();

		m_Width = (float)m_TextureWidth;
		m_Height = (float)m_TextureHeight;

		m_FixedWidth = GlIntToFixed(a_Image->GetWidth());
		m_FixedHeight = GlIntToFixed(a_Image->GetHeight());

		m_Vertices[ 0] = 0;            m_Vertices[ 1] = 0;
		m_Vertices[ 2] = m_FixedWidth; m_Vertices[ 3] = 0;
		m_Vertices[ 4] = m_FixedWidth; m_Vertices[ 5] = m_FixedHeight;

		m_Vertices[ 6] = 0;            m_Vertices[ 7] = 0;
		m_Vertices[ 8] = 0;            m_Vertices[ 9] = m_FixedHeight;
		m_Vertices[10] = m_FixedWidth; m_Vertices[11] = m_FixedHeight;

		m_UVs[ 0] = GlIntToFixed(0);    m_UVs[ 1] = GlIntToFixed(0);
		m_UVs[ 2] = GlIntToFixed(1);    m_UVs[ 3] = GlIntToFixed(0);
		m_UVs[ 4] = GlIntToFixed(1);    m_UVs[ 5] = GlIntToFixed(1);

		m_UVs[ 6] = GlIntToFixed(0);    m_UVs[ 7] = GlIntToFixed(0);
		m_UVs[ 8] = GlIntToFixed(0);    m_UVs[ 9] = GlIntToFixed(1);
		m_UVs[10] = GlIntToFixed(1);    m_UVs[11] = GlIntToFixed(1);

		YGC* pixels;
		if (m_Keying)
		{
			pixels = new YGC[a_Image->GetWidth() * a_Image->GetHeight()];
			YGC key = m_Key.GetColor();
			YGC mask = YG_COLOR_MASK_R | YG_COLOR_MASK_G | YG_COLOR_MASK_B;

			//Log::GetSingleton().Print("Key: %x", key);
			//Log::GetSingleton().Print("Mask: %x", mask);
			//Log::GetSingleton().Print("Color: %x", 0xFFFFFFFF & mask);

			YGC* src = (YGC*)a_Image->GetPixels();
			YGC* dst = pixels;

			for (int i = 0; i < a_Image->GetWidth() * a_Image->GetHeight(); i++)
			{
				if ((*src & mask) != key)
				{
					*dst = (*src & mask) | YG_COLOR_MASK_A;
				}
				else
				{
					*dst = *src & mask;
				}

				//*dst = *src & mask;
				dst++;
				src++;
			}
		}
		else
		{
			pixels = (YGC*)a_Image->GetPixels();
		}

		//Log::GetSingleton().Print("Dimensions: (%i x %i)", a_Image->GetWidth(), a_Image->GetHeight());

		glEnable(GL_TEXTURE_2D); CGLE();
		glBindTexture(GL_TEXTURE_2D, m_Texture->GetID()); CGLE();
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); CGLE();
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); CGLE();
			glTexImage2D(
				GL_TEXTURE_2D, 0, GL_RGBA, 
				a_Image->GetWidth(), a_Image->GetHeight(), 
				0, 
				GL_RGBA, GL_UNSIGNED_BYTE, pixels
			); CGLE();
		glDisable(GL_TEXTURE_2D); CGLE();

		//Log::GetSingleton().Print("Texture: %i", m_Texture->GetID());

		return true;
	}

	void SpriteOpenGLES::Reload()
	{
		//if (glIsTexture(m_Texture)) { glDeleteTextures(1, &m_Texture); }

		Log::GetSingleton().Print("Name: %s", m_Name);
		LoadImageData(m_Image, "");
	}

}